package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BibliotecaApp {

    private static final String NEW_LINE = "\n";

    public static void main(String[] args) throws IOException {

        LibraryManager libraryManager = new LibraryManager();
        libraryManager.setUserManager(generateUserManagerWithSampleUsers());
        libraryManager.setLibrary(generateLibraryWithSampleMovies());

        showOnCommandLine(libraryManager.getDisplay());

        while (true) {
            Option option = Option.getOptionFromValue(convertStringToInteger(getInputFromCommandLine()));

            switch (option) {
                case ADD_BOOK: addBook(libraryManager); break;
                case RESERVE_BOOK: reserveBook(libraryManager); break;
                case LOGIN: login(libraryManager); break;
                case EXIT: System.exit(0); break;
                default: libraryManager.select(option); break;
            }

            showOnCommandLine(libraryManager.getDisplay());
            showOnCommandLine(NEW_LINE);
        }
    }

    private static Library generateLibraryWithSampleMovies() {
        Library library = new Library();
        library.addMovie(new Movie("GREED", "Erich Von Stroheim", "1924"));
        library.addMovie(new Movie("THE CROWD", "King Vidor", "1928"));
        return library;
    }

    private static UserManager generateUserManagerWithSampleUsers() {
        UserManager userManager = new UserManager();
        User testUser1 = userManager.generateUser();
        testUser1.setPassword("1111");
        testUser1.setName("Felix Kitaka");
        testUser1.setEmail("test@gmail.com");
        testUser1.setPhone("0773267474");

        userManager.add(testUser1);
        return userManager;
    }

    private static void login(LibraryManager libraryManager) throws IOException {
        showOnCommandLine(TextConstants.USERNAME_LABEL);
        String username = getInputFromCommandLine();

        showOnCommandLine(TextConstants.PASSWORD_LABEL);
        String password = getInputFromCommandLine();

        libraryManager.select(Option.LOGIN, username, password);
    }

    private static void reserveBook(LibraryManager libraryManager) throws IOException {
        String title = "";
        if (!libraryManager.notAuthorized(Option.RESERVE_BOOK)) {
            showOnCommandLine(TextConstants.BOOK_TITLE_LABEL);
            title =  getInputFromCommandLine();
        }

        libraryManager.select(Option.RESERVE_BOOK, title);
    }

    private static void addBook(LibraryManager libraryManager) throws IOException {
        String title = "";
        String author = "";

        if (!libraryManager.notAuthorized(Option.ADD_BOOK)) {
            showOnCommandLine(TextConstants.BOOK_TITLE_LABEL);
            title = getInputFromCommandLine();

            showOnCommandLine(TextConstants.BOOK_AUTHOR_LABEL);
            author = getInputFromCommandLine();
        }

        libraryManager.select(Option.ADD_BOOK, title, author);
    }

    private static void showOnCommandLine(String s) {
        System.out.println(s);
    }

    private static String getInputFromCommandLine() throws IOException {
        return new BufferedReader(new InputStreamReader(System.in)).readLine();
    }

    private static Integer convertStringToInteger(String input) {
        Integer integerInput = 0;

        try { integerInput = new Integer(input); }
        catch (Exception e) {}

        return integerInput;
    }
}
